package com.java2007.hotel.dao.front.impl;

import com.java2007.hotel.dao.front.UserDao;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * 客户dao实现类
 * @author csl
 * @version V1.0
 * @date 2021/01/25
 **/
public class UserDaoImpl implements UserDao {
    /**
     *增加一个客户
     * @param user
     * @return
     */
    @Override
    public int add(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "INSERT INTO t_user\n" +
                "            (\n" +
                "             user_name,\n" +
                "             user_pwd,\n" +
                "             user_vip_status,\n" +
                "             user_create_date,\n" +
                "             admin_phone,\n" +
                "             admin_email)\n" +
                "VALUES (?,?,?,?,?,?)";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, user.getName(), user.getPwd(),
                user.getVipStatus(), user.getCreateDate(), user.getPhone(),
                user.getEmail());
        return update;
    }

    /**
     * 根据名字查询客户
     * @param name
     * @return
     */
    @Override
    public User selectByName(String name) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  user_id id,\n" +
                "  user_name name,\n" +
                "  user_pwd pwd ,\n" +
                "  user_vip_status vipStatus,\n" +
                "  user_create_date createDate,\n" +
                "  admin_phone phone,\n" +
                "  admin_email email," +
                " account\n" +
                "FROM t_user\n" +
                "WHERE user_name = ?";
        User query = queryRunner.query(sql, new BeanHandler<>(User.class), name);
        return query;
    }

    /**
     * 根据id查询客户
     * @param id
     * @return
     */
    @Override
    public User selectById(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  user_id id,\n" +
                "  user_name name,\n" +
                "  user_pwd pwd ,\n" +
                "  user_vip_status vipStatus,\n" +
                "  user_create_date createDate,\n" +
                "  admin_phone phone,\n" +
                "  admin_email email\n" +
                "FROM t_user\n" +
                "WHERE user_id = ?";
        User query = queryRunner.query(sql, new BeanHandler<>(User.class), id);
        return query;
    }


    /**
     * 根据id修改客户价钱
     * @param id
     * @return
     */
    @Override
    public int updateAccountById(int id, Long account) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "UPDATE t_user SET account = ? WHERE user_id = ?";
        int update = queryRunner.update(sql, account, id);
        return update;
    }

}
